# -*- encoding=utf8 -*-

__author__ = "code哥"
import asyncio
from sanic.blueprints import Blueprint
from sanic_openapi import doc
from conn_pool import get_redis, REDIS_APP_RUN_LOG

rs = get_redis()

blueprint = Blueprint(__name__, '/api')


@doc.summary("websocket 推送运行时日志")
async def feed(request, ws):
    while True:
        msg = rs.lpop(REDIS_APP_RUN_LOG)
        if msg is not None:
            await ws.send(str(msg))
        await asyncio.sleep(0.1)


blueprint.add_websocket_route(feed, '/run_log_ws')
